<?php

namespace api\controller;

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\DirectoryTemplateService;
use think\Exception;
use think\Request;

class DirectoryTemplateController extends VerifyController
{
    /**
     * @var DirectoryTemplateService
     *
     */
    protected $directoryTemplateService;


    public function __construct()
    {
        parent::__construct();
        $this->directoryTemplateService = new DirectoryTemplateService();
    }

    /**
     * 获取模板内容
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function getTemplateData(Request $request)
    {
        $param = $request->param();
        $resData = $this->directoryTemplateService->getTemplateData($param['filter']['id']);
        return json(success_response('', $resData));
    }

    public function delete(Request $request)
    {
        $param = $request->param();
        try {
            $resData = $this->directoryTemplateService->delete($param['param']['filter']);
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
        return json(success_response('', $resData));
    }

    /**
     * 获取模板内容的类型内容
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function getTemplateContentTypeData(Request $request)
    {
        $param = $request->param();
        $type = $param['param']['filter']['type'] ?? [];
        $resData = $this->directoryTemplateService->getTemplateContentTypeData($param['param']['filter']['id'], $type);
        return json(success_response('', $resData));
    }

    /**
     * 通过项目id获取模板内容
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function getTemplateContentTypeDataByProject(Request $request)
    {
        $param = $request->param();
        $type = $param['param']['filter']['type'] ?? [];
        unset($param['param']['filter']['type']);
        $param['param']['filter']['tenant_id'] = $param['param']['filter']['tenant_id'] ?? $request->getTenantId();
        $resData = $this->directoryTemplateService->getTemplateContentItemDataByProject($param['param']['filter'], $type);
        return json(success_response('', $resData));
    }

    /**
     * 获取默认的目录模板
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function getOptionsTemplate(Request $request)
    {
        $resData = $this->directoryTemplateService->getOptionsTemplate();
        return json(success_response('', $resData));
    }

    /**
     * 创建类型
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function saveType(Request $request)
    {
        $param = $request->param();
        $content = $param['data']['content'] ?? [];
        $resData = $this->directoryTemplateService->saveType($param['data']['id'], $param['data']['type'], $content);
        return json(success_response('', $resData));
    }

    /**
     * 删除项目模板的类型
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function deleteType(Request $request)
    {
        $param = $request->param();
        $resData = $this->directoryTemplateService->deleteType($param['param']['filter']['id'], $param['param']['filter']['type']);
        return json(success_response('', $resData));
    }


    /**
     * 添加类型下的子项
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function saveItem(Request $request)
    {
        $param = $request->param();
        $resData = $this->directoryTemplateService->saveItem($param['param']['filter']['id'], $param['param']['filter']['type'], $param['data']);
        return json(success_response('', $resData));
    }

    /**
     * 删除类型下的子项
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function deleteItem(Request $request)
    {
        $param = $request->param();
        $resData = $this->directoryTemplateService->deleteItem($param['param']['filter']['id'], $param['param']['filter']['type'], $param['param']['filter']['dir_name']);
        return json(success_response('', $resData));
    }
}
